package com.fhd.dao.Client;

import com.fhd.dao.BaseDao;
import com.fhd.pojo.Client;
import com.mysql.cj.util.StringUtils;

import java.sql.*;
import java.util.ArrayList;

public class ClientDaoImplement implements ClientDao{
    // 通过账号得到客户
    @Override
    public Client getLoginClient(Connection conn, String userCode) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Client client = new Client();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select * from client where user_code = ?";
                Object[] parameters = {userCode};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                if(rs.next()) {
                    client.setId(rs.getInt("id"));
                    client.setUserCode(rs.getString("user_code"));
                    client.setUserPassword(rs.getString("user_password"));
                    client.setUserName(rs.getString("user_name"));
                    client.setRoleId(rs.getInt("role_id"));
                    client.setMoney(rs.getInt("money"));
                    client.setPoints(rs.getInt("points"));
                    client.setLastPointsDate(rs.getDate("last_points_date"));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return client;
    }

    // 注册客户账号
    @Override
    public int signUpClient(Connection conn, String userCode, String userPassword, String userName) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "insert into client (user_code, user_password, user_name,last_points_date) values (?,?,?,?)";
                Object[] parameters = {userCode,userPassword,userName,"2001-01-01 00:00:00"};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 修改个人信息
    @Override
    public int updateInformation(Connection conn,int id,String userCode, String userPassword, String userName) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        ArrayList<Object> objects = new ArrayList<>();
        try {
            if (conn != null) {
                // 通过前端传来的数据拼接sql语句,并把参数先放进ArrayList中
                StringBuffer sql = new StringBuffer("update client set ");
                if (!StringUtils.isNullOrEmpty(userCode)) {
                    sql.append("user_code = ?,");
                    objects.add(userCode);
                }
                if (!StringUtils.isNullOrEmpty(userPassword)) {
                    sql.append("user_password = ?,");
                    objects.add(userPassword);
                }
                if (!StringUtils.isNullOrEmpty(userName)) {
                    sql.append("user_name = ?,");
                    objects.add(userName);
                }
                sql.deleteCharAt(sql.lastIndexOf(","));
                sql.append(" where id = ?");
                objects.add(id);

                Object[] parameters = objects.toArray();
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql.toString(),parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 更改上次签到时间和积分
    @Override
    public int updateLastPointsDate(Connection conn, int id, int points,Date newDate) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "update client set points = ?,last_points_date = ? where id = ?";
                Object[] parameters = {points,newDate,id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 增加/减少余额
    @Override
    public int updateMoney(Connection conn, int id, int money) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "update client set money = money + ? where id = ?";
                Object[] parameters = {money,id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 重新获取余额
    @Override
    public int getMoney(Connection conn, int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int money = -1;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select money from client where id = ?";
                Object[] parameters = {id};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);
                if(rs.next()) {
                    money = rs.getInt("money");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return money;
    }
}
